const city = require('./city.min.json');
const province = require('./province.min.json');
const names = {"上海": "上海市", "云南": "云南省", "北京": "北京市", "吉林": "吉林省", "四川": "四川省", "天津": "天津市", "宁夏": "宁夏回族自治区", "安徽": "安徽省", "山东": "山东省", "山西": "山西省", "广东": "广东省", "广西": "广西壮族自治区", "新疆": "新疆维吾尔自治区", "江苏": "江苏省", "江西": "江西省", "河北": "河北省", "河南": "河南省", "浙江": "浙江省", "海南": "海南省", "湖北": "湖北省", "湖南": "湖南省", "甘肃": "甘肃省", "福建": "福建省", "西藏": "西藏自治区", "贵州": "贵州省", "辽宁": "辽宁省", "重庆": "重庆市", "陕西": "陕西省", "青海": "青海省", "内蒙古": "内蒙古自治区", "黑龙江": "黑龙江省"};

/**
 * 从字符串取得地址信息函数 精确到区级
 * @param {detail} 地址字符串
 * @returns 地址信息字典
 */
function address_info(address) {
    var info = city_info(address);
    if (!info) {
        return null;
    }
    if (info['level'] === '省级') {
        // 只分析到省级
        return info;
    }
    var short_name = info['name'];
    var detail = info['detail'];
    var tmp = city[short_name];
    if (!tmp) {
        return null;
    }
    if (tmp["_"]) {
        return {level: '区级', "name": short_name, "code": tmp["_"]["_"][1], "detail": detail};
    }
    if (tmp[detail[0]]) {
        for (var index in tmp[detail[0]]) {
            if (detail.indexOf(index) === 0) {
                var full_name = tmp[detail[0]][index][0];
                if (detail.indexOf(full_name) === 0) {
                    var detail2 = detail.substr(full_name.length).trim();
                } else {
                    var detail2 = detail.substr(index.length).trim();
                }
                return {"level": "区级","name": index, "code": tmp[detail[0]][index][1], "detail": detail2};
            }
        }
    }
    // 只分析到市级
    return info;
}

/**
 * 从字符串取得城市信息函数
 * @param {detail} 地址字符串
 * @returns 城市信息字典
 */
function city_info(detail) {
    detail = detail.trim();
    if (!detail) {
        return null;
    }
    var fn = function(tmp2, detail2, index) {
        if (detail2 && tmp2[detail2[0]]) {
            // 取得市级信息第一个汉字索引 并循环
            for (var city_short_name in tmp2[detail2[0]]) {
                if (detail2.indexOf(city_short_name) === 0) {
                    var city_full_name = tmp2[detail2[0]][city_short_name];
                    if (detail2.indexOf(city_full_name) === 0) {
                        var detail3 = detail2.substr(city_full_name.length).trim();
                    } else {
                        var detail3 = detail2.substr(city_short_name.length).trim();
                    }
                    return {'level': '市级', 'name': city_short_name, 'detail': detail3};
                }
            }
        }
        if (index) {
            return {'level': '省级', 'name': index, 'detail': detail2};
        }
    };
    // 取得省份第一个汉字索引
    var tmp = province["province"][detail[0]];
    if (tmp) {
        for (var index in tmp) {
            if (detail.indexOf(index) === 0) {
                var tmp2 = tmp[index];
                if (Object.keys(tmp2).length === 0) {
                    // 直辖市
                    break;
                }
                if (detail.indexOf(names[index]) === 0) {
                    var detail2 = detail.substr(names[index].length).trim();
                } else if (names[index].indexOf('自治区') > 0 && detail.indexOf(`${index}自治区`) === 0) {
                    var detail2 = detail.substr(`${index}自治区`.length).trim();
                } else {
                    var detail2 = detail.substr(index.length).trim();
                }
                result = fn(tmp2, detail2, index);
                if (result) {
                    return result;
                }
            }
        }
    }
    return fn(province["city"], detail);
}

module.exports = {
    city_info,
    address_info
};